package api

import (
	"github.com/jinzhu/copier"
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/redis/cache"
	"longmen/server/pkg/grpc_client"
	pb "longmen/server/pkg/protobuf"
)

// @Summary 房管私聊-获取历史记录
// @Description 房管私聊-获取历史记录
// @Accept  json
// @Produce  json
// @Tags MgrChat
// @Param   sendBody body api_models.GetMgrChatHistoryReq true "body"
// @Success 200 {object} api_models.GetMgrChatHistoryResp "result"
// @Router /api/mgrChat/getHistory [POST]
func (base *Controller) GetMgrChatHistory(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	req := api_models.GetMgrChatHistoryReq{}
	if err := base.BindAndValid(c, &req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	resp, err := grpc_client.GrpcTencentImClient.GetMgrChatHistory(ctx, &pb.GetMgrChatHistoryReq{
		SelfUserId: uint32(user.Id),
		UserId:     uint32(req.UserId),
		Page:       uint32(req.Page),
		Limit:      uint32(req.Limit),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}
	if len(resp.MgrChatHistory) == 0 {
		resp.MgrChatHistory = []*pb.MgrChatHistory{}
	}
	res := api_models.GetMgrChatHistoryResp{
		MgrChatHistory: []api_models.MgrChatHistory{},
	}
	copier.Copy(&res, &resp)
	return util.BuildSuccess(c, res, "")
}

// @Summary 房管私聊-清空历史
// @Description 房管私聊-清空历史
// @Accept  json
// @Produce  json
// @Tags MgrChat
// @Param   sendBody body api_models.DelMgrChatHistoryReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/mgrChat/delHistory [POST]
func (base *Controller) DelMgrChatHistory(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	req := api_models.DelMgrChatHistoryReq{}
	if err := base.BindAndValid(c, &req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	_, err = grpc_client.GrpcTencentImClient.DelMgrChatHistory(ctx, &pb.DelMgrChatHistoryReq{
		SelfUserId: uint32(user.Id),
		UserId:     uint32(req.UserId),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}

	cache.ResetMgrChatUserReadNum(user.Id, req.UserId)

	return util.BuildSuccess(c, nil, "")
}

// @Summary 房管私聊-删除聊天
// @Description 房管私聊-删除聊天
// @Accept  json
// @Produce  json
// @Tags MgrChat
// @Param   sendBody body api_models.DelMgrChatReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/mgrChat/delChat [POST]
func (base *Controller) DelMgrChat(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	req := api_models.DelMgrChatReq{}
	if err := base.BindAndValid(c, &req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	ctx, cancel := base.GrpcWithTimeout()
	defer cancel()
	_, err = grpc_client.GrpcTencentImClient.DelMgrChat(ctx, &pb.DelMgrChatReq{
		SelfUserId: uint32(user.Id),
		UserId:     uint32(req.UserId),
	})
	if err != nil {
		return util.BuildFail(c, 1, err.Error(), err)
	}

	cache.ResetMgrChatUserReadNum(user.Id, req.UserId)

	return util.BuildSuccess(c, nil, "")
}

// @Summary 重置房管私聊未读计数
// @Description 重置房管私聊未读计数
// @Accept  json
// @Produce  json
// @Tags MgrChat
// @Param   sendBody body api_models.MgrChatReadReq true "body"
// @Success 200 {object} api_models.Response "result"
// @Router /api/mgrChat/read [POST]
func (base *Controller) MgrChatRead(c echo.Context) error {
	user, err := base.GetUserByCtx(c)
	if err != nil {
		return util.BuildLoginTimeOut(c, err)
	}

	req := &api_models.MgrChatReadReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BuildFail(c, 1, "", err)
	}

	cache.ResetMgrChatUserReadNum(user.Id, req.UserId)

	return util.BuildSuccess(c, nil, "")
}
